//
//  RegistryViewController.m
//  Politics
//
//  Created by Hai Nguyen on 7/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RegistryViewController.h"
#import "LoginViewController.h"


@implementation RegistryViewController

@synthesize controller;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
	status = -1;
    [super viewDidLoad];
	[self.view addSubview:scrollView];
	[scrollView setFrame:CGRectMake(0, 0, 320, 480)];
	[scrollView setContentSize:CGSizeMake(320, 700)];
}

- (IBAction) buttonPressed:(id) sender
{
	[Audio playButtonClicked];
	UIButton *button = (UIButton*) sender;
	switch (button.tag)
	{
		case 1:
		{
			status = RegisterSusccess;
			[self submitRegistry];
			//[self.navigationController popViewControllerAnimated:YES];
		}
			break;
		case 3:
		{
			/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Are you sure ?" delegate:self cancelButtonTitle:@"Sure" otherButtonTitles:@"Close",nil];
			[alert show];
			[alert];*/
			[controller finishRegistryWithStatus:RegisterCancel];
		}
			break;
		case 5:
		{
			[maleCheckButton setSelected:YES];
			[femaleCheckButton setSelected:NO];
		}
			break;
		case 6:
		{
			[maleCheckButton setSelected:NO];
			[femaleCheckButton setSelected:YES];
		}
			break;
		default:
			break;
	}
}
- (void) submitRegistry
{
	if ([usernameTextField.text length]==0) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
														message:@"Username should not empty" 
													   delegate:nil 
											  cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	if ([passwordTextField.text length]==0) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
														message:@"Password should not empty" 
													   delegate:nil 
											  cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	if ([emailTextField.text length]==0) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
														message:@"Email should not empty" 
													   delegate:nil 
											  cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] init];
	[infoDict setObject:usernameTextField.text forKey:@"username"];
	[infoDict setObject:passwordTextField.text forKey:@"password"];
	[infoDict setObject:emailTextField.text forKey:@"email"];
	[infoDict setObject:firstNameTextField.text forKey:@"firstname"];
	[infoDict setObject:lastNameTextField.text forKey:@"lastname"];
	[infoDict setObject:cityTextField.text forKey:@"city"];
	[infoDict setObject:stateTextField.text forKey:@"state"];
	if (maleCheckButton.selected)
	{
		[infoDict setObject:@"1" forKey:@"gender"];
	}else {
		[infoDict setObject:@"0" forKey:@"gender"];
	}
	RegistryParser *registryParser = [[RegistryParser alloc] init];
	[registryParser registryWithInfo:infoDict withDelegate:self];
	[infoDict release];
}
#pragma mark -
- (void)didEndRegistry:(int)result
{
	NSLog(@"Registry result = %d",result);
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark UIAlertViewDelegate Methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	switch (buttonIndex)
	{
		case 0:
			//[self.navigationController popViewControllerAnimated:YES];
			[controller finishRegistryWithStatus:status];
			break;
		case 1:
			//
			break;
		default:
			break;
	}
	[alertView release];
}
#pragma mark -
#pragma mark TextField Delegate Methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	/*switch (textField.tag)
	{
		case 6:
			[scrollView scrollRectToVisible:CGRectMake(0, 30, 320, 480) animated:YES];
			break;
		case 7:
			[scrollView scrollRectToVisible:CGRectMake(0, 60, 320, 480) animated:YES];
			break;
		case 8:
			[scrollView scrollRectToVisible:CGRectMake(0, 90, 320, 480) animated:YES];
			break;
		case 9:
			[scrollView scrollRectToVisible:CGRectMake(0, 120, 320, 480) animated:YES];
			break;
		default:
			break;
	}*/
	return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	/*switch (textField.tag)
	{
		case 1:
			[passwordTextField becomeFirstResponder];
			break;
		case 2:
			[emailTextField becomeFirstResponder];
			break;
		case 4:
			[firstNameTextField becomeFirstResponder];
			break;
		case 5:
			[lastNameTextField becomeFirstResponder];
			break;
		case 6:
			[cityTextField becomeFirstResponder];
			break;
		case 8:
			[stateTextField becomeFirstResponder];
			break;
		case 9:
			[textField resignFirstResponder];
			[scrollView scrollRectToVisible:CGRectMake(0, 5, 320, 480) animated:YES];
			break;
		default:
			break;
	}*/
	return YES;
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
